#include "buffer.h"
#include "string.h"

Buffer bufferData[10] = {0};
Buffer* pAddBuffer;
Buffer* pDecBuffer;


void bufferInit()
{
  uint8_t i = 0;
  
  for(i = 0; i < 9; i++)
  {
    bufferData[i].pNext = &bufferData[i+1];
  }
  bufferData[9].pNext = bufferData;
  pDecBuffer = pAddBuffer = bufferData;
}

uint8_t addBuffer(UART_BUF_R *uartBuffer)
{
  if(pAddBuffer->pNext == pDecBuffer)
    return 0;
  memcpy(&pAddBuffer->uart0RxBuf, uartBuffer, sizeof(UART_BUF_R));
  pAddBuffer = pAddBuffer->pNext;
  return 1;
}

uint8_t getBuffer(UART_BUF_R *uartBuffer)
{
  __disable_irq();
  if(pDecBuffer == pAddBuffer)
  {
    __enable_irq();
    return 0;
  }
  memcpy(uartBuffer, &pDecBuffer->uart0RxBuf, sizeof(UART_BUF_R));
  pDecBuffer = pDecBuffer->pNext;
  __enable_irq();
  return 1;
}